package org.hxyjs.dao.impl;

import org.hxyjs.dao.SchoolDao;
import org.hxyjs.entity.School;
import org.hxyjs.mapper.SchoolMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("schoolDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class SchoolDaoImpl implements SchoolDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private SchoolMapper schoolMapper;
    public List<School>  getAllSchools() throws Exception{
        return schoolMapper.selectByExample(null);
    }

    @Override
    public Integer deleteSchoolBySno(String sno)  throws Exception{
        return schoolMapper.deleteByPrimaryKey(sno);
    }

    @Override
    public Integer updateSchoolBySno(School school)  throws Exception{
        return schoolMapper.updateByPrimaryKey(school);
    }

    @Override
    public Integer addSchool(School school)  throws Exception{
        school.setSno(initId());
        return schoolMapper.insertSelective(school);
    }
    public String initId(){
        String maxSno = schoolMapper.getMaxSno();
        return GenNumTools.initId("",3,maxSno);
    }
    @Override
    public List<School> getSchoolByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)schoolMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return schoolMapper.getSchoolByPage(map);
    }
}
